package com.hellobike.haxing.modules.wms.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.hellobike.haxing.common.vo.Result;
import com.hellobike.haxing.common.system.query.QueryGenerator;
import com.hellobike.haxing.common.util.oConvertUtils;
import com.hellobike.haxing.modules.product.entity.Sku;
import com.hellobike.haxing.modules.product.entity.Spu;
import com.hellobike.haxing.modules.product.service.ISkuService;
import com.hellobike.haxing.modules.product.service.ISpuService;
import com.hellobike.haxing.modules.shop.entity.Shop;
import com.hellobike.haxing.modules.shop.service.IShopService;
import com.hellobike.haxing.modules.wms.dto.ReturnWarehouseDetailDto;
import com.hellobike.haxing.modules.wms.entity.ReturnWarehouseDetail;
import com.hellobike.haxing.modules.wms.entity.WarehouseShop;
import com.hellobike.haxing.modules.wms.enums.ReturnStatusEnum;
import com.hellobike.haxing.modules.wms.enums.ReturnTypeEnum;
import com.hellobike.haxing.modules.wms.exception.WmsServiceException;
import com.hellobike.haxing.modules.wms.facade.WmsReturnFacade;
import com.hellobike.haxing.modules.wms.service.IReturnWarehouseDetailService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hellobike.haxing.modules.wms.service.IWarehouseShopService;
import com.hellobike.haxing.modules.wms.utils.ListUtil;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import com.hellobike.haxing.common.system.base.controller.BaseController;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import com.hellobike.haxing.common.annotation.AutoLog;
 /**
 * @Description: t_wms_return_warehouse_detail
 * @Author: haxing-boot
 * @Date:   2020-02-07
 * @Version: V1.0
 */
@RestController
@RequestMapping("/wms/returnWarehouseDetail")
@Slf4j
public class ReturnWarehouseDetailController extends WmsBaseController<ReturnWarehouseDetail, IReturnWarehouseDetailService> {
	@Autowired
	private IReturnWarehouseDetailService returnWarehouseDetailService;
	@Autowired
	private ISkuService skuService;
	@Autowired
	private ISpuService spuService;
	
	/**
	 * 分页列表查询
	 *
	 * @param returnWarehouseDetail
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "t_wms_return_warehouse_detail-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(ReturnWarehouseDetail returnWarehouseDetail,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		returnWarehouseDetail.setReturnStatus(ReturnStatusEnum.INIT.getValue());//只查询初始化的
		returnWarehouseDetail.setWarehouseId(getWarehouseId());
		QueryWrapper<ReturnWarehouseDetail> queryWrapper = QueryGenerator.initQueryWrapper(returnWarehouseDetail, req.getParameterMap());
		Page<ReturnWarehouseDetail> page = new Page<ReturnWarehouseDetail>(pageNo, pageSize);
		IPage<ReturnWarehouseDetail> pageList = returnWarehouseDetailService.page(page, queryWrapper);
		IPage<ReturnWarehouseDetailDto> result = wmsReturnFacade.wrapperPageWarehouseDetail(pageList);
		return Result.ok(result);
	}
	
	/**
	 *   添加
	 *
	 * @param returnWarehouseDetail
	 * @return
	 */
	@AutoLog(value = "t_wms_return_warehouse_detail-添加",operateType=2)
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody ReturnWarehouseDetail returnWarehouseDetail) {
		returnWarehouseDetailService.save(returnWarehouseDetail);
		return Result.ok("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param returnWarehouseDetail
	 * @return
	 */
	@AutoLog(value = "t_wms_return_warehouse_detail-编辑",operateType=3)
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody ReturnWarehouseDetail returnWarehouseDetail) {
		returnWarehouseDetailService.updateById(returnWarehouseDetail);
		return Result.ok("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "t_wms_return_warehouse_detail-删除",operateType=4)
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		returnWarehouseDetailService.removeById(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "t_wms_return_warehouse_detail-批量删除",operateType=4)
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.returnWarehouseDetailService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		ReturnWarehouseDetail returnWarehouseDetail = returnWarehouseDetailService.getById(id);
		if(returnWarehouseDetail==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(returnWarehouseDetail);
	}
	 @GetMapping(value = "/queryByHeaderId")
	 public Result<?> queryByHeaderId(@RequestParam(name="id",required=true) Long id) {
		 List<ReturnWarehouseDetail> details = returnWarehouseDetailService.getByHeaderId(id);
		 if(ListUtil.isNullOrEmpty(details)) {
			 return Result.error("未找到对应数据");
		 }
		 List<ReturnWarehouseDetailDto> result = new ArrayList<>();
		 for (ReturnWarehouseDetail record : details) {
			 ReturnWarehouseDetailDto dto = new ReturnWarehouseDetailDto();
			 BeanUtils.copyProperties(record,dto);
			 Shop shop = shopService.getById(record.getShopId());
			 dto.setShopName(shop.getShopName());
			 if(ReturnTypeEnum.SKU.getValue().equals(record.getReturnType())){
				 Sku sku = skuService.getById(record.getSkuId());
				 if(null != sku){
					 dto.setBarcode(sku.getBarcode());
					 dto.setSkuCode(sku.getSkuCode());
					 Spu spu = spuService.getById(sku.getSpuId());
					 dto.setSpuCode(spu.getSpuCode());
					 dto.setSpuName(spu.getSpuName());
					 dto.setDimensionValues(sku.getDimensionValues());
				 }
			 }
			 result.add(dto);
		 }
		 return Result.ok(result);
	 }
    /**
    * 导出excel
    *
    * @param request
    * @param returnWarehouseDetail
    */
    @AutoLog(value = "t_wms_return_warehouse_detail-导出excel",operateType=6)
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ReturnWarehouseDetail returnWarehouseDetail) {
        return super.exportXls(request, returnWarehouseDetail, ReturnWarehouseDetail.class, "t_wms_return_warehouse_detail");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @AutoLog(value = "t_wms_return_warehouse_detail-导入excel",operateType=5)
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ReturnWarehouseDetail.class);
    }

    @Autowired
	private IWarehouseShopService warehouseShopService;
    @Autowired
	private IShopService shopService;
    @Autowired
	private WmsReturnFacade wmsReturnFacade;

	 @GetMapping(value = "/getShops")
	 public Result<?> getShops() {
		 List<Shop> shops = new ArrayList<>();
		 WarehouseShop param =new WarehouseShop();
		 param.setWarehouseId(getWarehouseId());
		 List<WarehouseShop> warehouseShops = warehouseShopService.getBaseMapper().selectList(new QueryWrapper<>(param));
		 if (ListUtil.isNotEmpty(warehouseShops)) {
			 for (WarehouseShop warehouseShop : warehouseShops) {
				 Shop shop = shopService.getById(warehouseShop.getShopId());
				 if(null != shop){
					 shops.add(shop);
				 }
			 }
		 }
		 return Result.ok(shops);
	 }

	 @AutoLog(value = "t_wms_return_warehouse_detail-publishReturnWarehouseOrder",operateType=2)
	 @PostMapping(value = "/publishReturnWarehouseOrder")
	 public Result<?> publishReturnWarehouseOrder(@RequestBody JSONObject param) {
		 List<Long> ids = param.getJSONArray("ids").toJavaList(Long.class);
		 try {
			 wmsReturnFacade.publishReturnWarehouseOrder(ids,getWarehouseId(),getSysUserId());
		 } catch (WmsServiceException e) {
			 log.error("publishReturnWarehouseOrder-error",e);
			 return Result.error(e.getMessage());
		 }
		 return Result.ok("添加成功！");
	 }


}
